import Vuex from "vuex";
import Vue from "vue";
import tabBar from "./tabBar";
import activity from "./activity";
import agent from "./agent";
import {
    getStorageObj,
    setStorageObj,
    getSessionStorageObj,
    setSessionStorageObj,
    getDeviceInfo,
    getAid
} from "../js/utils";
import liveImg from "@/assets/直播间.jpg";
import { getStaffInfo, getStaffDataCount, exportData } from "@/api/staff";
/* eslint-disable */

Vue.use(Vuex);
export const store = new Vuex.Store({
    state: {
        jsapiTicket: {},
        userId: getSessionStorageObj("userId"),
        subscribe: getSessionStorageObj("subscribe"),
        shareParams: getSessionStorageObj("shareParams"),
        activityMsg: getSessionStorageObj("activityMsg"),
        loading: false,
        device: getDeviceInfo(),
        userInfo: getSessionStorageObj("userInfo"),
        inited: getSessionStorageObj("inited"),
        appData: {},
        staffInfo: getSessionStorageObj("staffInfo"),
        liveImg: liveImg,
        aid: getAid()
    },
    mutations: {
        setJsapiTicket(store, ticket) {
            store.jsapiTicket = ticket;
        },
        setAppData(state, payload) {
            state.appData = payload;
            // setSessionStorageObj("appData", payload);
        },
        setUserId(store, userId) {
            store.userId = userId;
            setSessionStorageObj("userId", userId);
        },
        setStaffInfo(store, payload) {
            store.staffInfo = payload;
            setSessionStorageObj("staffInfo", payload);
        },
        clearStaffInfo(store) {
            store.staffInfo = null;
            sessionStorage.removeItem("staffInfo");
        },
        setUserInfo(store, userInfo) {
            store.userInfo = userInfo;
            store.userId = userInfo.openId;
            setSessionStorageObj("userId", userInfo.openId);
            setSessionStorageObj("userInfo", userInfo);
        },
        setSubscribe(store, subscribe) {
            store.subscribe = subscribe;
            setSessionStorageObj("subscribe", subscribe);
        },
        setShareParams(store, params) {
            console.log("setShareParams", params);
            store.shareParams = params;
            setSessionStorageObj("shareParams", params);
        },
        setActivityMsg(store, payload) {
            store.activityMsg = payload;
            setSessionStorageObj("activityMsg", payload);
        },
        showLoading(state) {
            state.loading = true;
        },
        hideLoading(state) {
            state.loading = false;
        },
        setInit(state) {
            state.inited = true;
            setSessionStorageObj("inited", true);
        }
    },
    actions: {
        getStaffInfo({ commit }, payload) {
            return new Promise((resolve, reject) => {
                getStaffInfo(payload)
                    .then(res => {
                        if (res.code == "000000") {
                            commit("setStaffInfo", res.data);
                            resolve(res);
                        } else {
                            commit("clearStaffInfo");
                            resolve(undefined);
                        }
                    })
                    .catch(err => {
                        reject(err);
                    });
            });
        },
        getStaffDataCount({ commit }, payload) {
            return new Promise((resolve, reject) => {
                getStaffDataCount(payload)
                    .then(res => {
                        resolve(res);
                    })
                    .catch(err => {
                        reject(err);
                    });
            });
        },
        exportData({ commit }, payload) {
            return new Promise((resolve, reject) => {
                exportData(payload)
                    .then(res => {
                        resolve(res);
                    })
                    .catch(err => {
                        reject(err);
                    });
            });
        }
    },
    modules: {
        tabBar,
        activity,
        agent
    }
});
